package org.keyyond.ms.commonservice.common;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import com.github.pagehelper.Page;

@SuppressWarnings({"rawtypes", "unchecked"})
public class ResultInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    //当前页
    private int start;
    //每页的数量
    private int number;
    //总记录数
    private long totalItemCount;
    //总页数
    private int numberOfPages;
    //结果集
    private List<T> list;

    public ResultInfo() {
    }

    /**
     * 包装Page对象
     *
     * @param list
     */
    public ResultInfo(List<T> list) {
        if (list instanceof Page) {
            Page page = (Page) list;
            this.start = page.getPageNum();
            this.number = page.getPageSize();

            this.numberOfPages = page.getPages();
            this.list = page;
            this.totalItemCount = page.getTotal();
        } else if (list instanceof Collection) {
            this.start = 1;
            this.number = list.size();

            this.numberOfPages = 1;
            this.list = list;
            this.totalItemCount = list.size();
        }
    }

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public long getTotalItemCount() {
		return totalItemCount;
	}

	public void setTotalItemCount(long totalItemCount) {
		this.totalItemCount = totalItemCount;
	}

	public int getNumberOfPages() {
		return numberOfPages;
	}

	public void setNumberOfPages(int numberOfPages) {
		this.numberOfPages = numberOfPages;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	@Override
	public String toString() {
		return "ResultInfo [start=" + start + ", number=" + number + ", totalItemCount=" + totalItemCount
				+ ", numberOfPages=" + numberOfPages + ", list=" + list + "]";
	}
	
//	public Map<Object,Object> getResult() {
//		Map<Object, Object> resultMap = new HashMap<>();
//		JSONArray records =JSONArray.fromObject(this.list);
//		resultMap.put("records", records);
//		resultMap.put("start", this.getStart());
//		resultMap.put("number", this.getnumber());
//		resultMap.put("pagescount", this.getPagescount());
//		resultMap.put("totalcount", this.getTotalcount());
//		return resultMap;
//	}

}